package com.hl.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.hl.domain.Course.*;
import com.hl.domain.User;
import com.hl.mapper.CourseMapper;
import com.hl.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public Course selectCourseById(int i) {
        Course course = courseMapper.selectByPrimaryKey((long) i);
        if (course != null) {
            course.setTotal(courseMapper.selectCountOfCourse());
            CoursePrice coursePrice = courseMapper.selectCoursePriceById(Math.toIntExact(course.getId()));
            CourseHot courseHot = courseMapper.selectCourseHotById(Math.toIntExact(course.getId()));
            if (coursePrice != null) {
                course.setCoursePrice(coursePrice);
            }
            if (courseHot != null) {
                course.setCourseHot(courseHot);
            }
        }

        return course;
    }

    @Override
    public List<Course> selectCourseByPage(int PageNum, int PageSize) {
        PageHelper.startPage(PageNum, PageSize);
        List<Course> courses = courseMapper.selectALlCourse();
        for (Course course : courses) {
            if (course != null) {
                course.setTotal(courseMapper.selectCountOfCourse());
                CoursePrice coursePrice = courseMapper.selectCoursePriceById(Math.toIntExact(course.getId()));
                CourseHot courseHot = courseMapper.selectCourseHotById(Math.toIntExact(course.getId()));
                if (coursePrice != null) {
                    course.setCoursePrice(coursePrice);
                }
                if (courseHot != null) {
                    course.setCourseHot(courseHot);
                }
            }
        }

        return courses;
    }



    @Override
    public int insertCourse(Course course) {
        return courseMapper.insertSelective(course);
    }

    @Override
    public int deleteCourse(int i) {
        return courseMapper.deleteByPrimaryKey((long) i);
    }

    @Override
    public int updateCourse(Course course) {
        return courseMapper.updateByPrimaryKeySelective(course);
    }

    @Override
    public List<Coursemark> selectAllCourseMark(int i) {
        return courseMapper.selectAllCourseMark(i);
    }

    @Override
    public Map<String, Object> selectCourseTeacherById(int i) {
        CourseTeacher teacher = courseMapper.selectCourseTeacherById(i);
        List<CourseBook> books = courseMapper.selectCourseTeacherBook(i);
        List<CourseGood> goods = courseMapper.selectCourseTeacherGood(i);
        List<Course> courses = courseMapper.selectCourseByTeacherId(i);
        for (Course course : courses) {
            if (course!=null){
                CoursePrice price = courseMapper.selectCoursePriceById(Math.toIntExact(course.getId()));
                course.setCoursePrice(price);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("teacherBasdInfo", teacher);
        map.put("teacherGood", goods);
        map.put("teacherBook", books);
        map.put("teacherCourse",courses);
        return map;
    }



    @Override
    public List<CourseProblem> selectCourseProblemById(int i) {
        List<CourseProblem> courseProblems = courseMapper.selectCourseProblem(i);
        for (CourseProblem courseProblem : courseProblems) {
            if (courseProblem.getReply() != null) {
                List<CourseReply> replies = courseProblem.getReply();
                Date lastReplyTime = courseMapper.selectCourseProblemLastReplyTime(Math.toIntExact(courseProblem.getId()));
                courseProblem.setLastReplyTime(lastReplyTime);
                for (CourseReply reply : replies) {
                    if (reply != null) {
                        int userId = Math.toIntExact(reply.getUserId());
                        CourseUser courseUser = courseMapper.selectCourseUserById(userId);
                        reply.setReplyUser(courseUser);
                    }
                }
            }
        }
        return courseProblems;
    }

    @Override
    public List<Map<String, Object>> selectAllCourseTeachers() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<CourseTeacher> teachers = courseMapper.selectAllCourseTeachers();
        for (CourseTeacher teacher : teachers) {
            if (teacher!=null){
                List<CourseBook> books = courseMapper.selectCourseTeacherBook(Math.toIntExact(teacher.getId()));
                List<CourseGood> goods = courseMapper.selectCourseTeacherGood(Math.toIntExact(teacher.getId()));
                Map<String, Object> map = new HashMap<>();
                map.put("teacherBasdInfo", teacher);
                map.put("teacherGood", goods);
                map.put("teacherBook", books);
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public List<Course> selectALlCourseByOptions(int PageNum, int PageSize ,String courseName,Long teacherId) {
        PageHelper.startPage(PageNum,PageSize);
        List<Course> courses = courseMapper.selectAllCourseByOptions(courseName,teacherId);
        for (Course course : courses) {
            if (course!=null){
                course.setTotal(courseMapper.selectCountOfCourse());
                course.setTotal(courseMapper.selectCountOfCourse());
                CoursePrice coursePrice = courseMapper.selectCoursePriceById(Math.toIntExact(course.getId()));
                CourseHot courseHot = courseMapper.selectCourseHotById(Math.toIntExact(course.getId()));
                CourseTeacher courseTeacher = courseMapper.selectCourseTeacherById(Math.toIntExact(course.getTeacherId()));
                course.setCourseTeacher(courseTeacher);
                if (coursePrice != null) {
                    course.setCoursePrice(coursePrice);
                }
                if (courseHot != null) {
                    course.setCourseHot(courseHot);
                }
            }
        }
        return courses;
    }

    @Override
    public int setPrice(Long courseId, Double price, Double discount) {
        int isExit = courseMapper.selectNumOfCoursePriceById(courseId);
        if (isExit >0){
            // 价格存在就修改
            return courseMapper.updateCoursePriceById(courseId,price,discount);
        }else {
            // 价格不存在就添加
            return courseMapper.addCoursePrice(courseId,price,discount);
        }
    }

    @Override
    public Map<String, Object> Login(String username, String password) {
        User user = courseMapper.selectUseByUserName(username);
        Map<String,Object> map = new HashMap<>();
        if (user !=null){
            // 用户存在
            if (password.equals(user.getPassword())){
                // 用户密码正确
                map.put("code",1);
                map.put("msg","校验正确");
                map.put("username",user.getUsername());
                map.put("nickname",user.getNickname());
                map.put("Id",user.getId());
            }else {
                // 输入密码错误
                map.put("code",2);
                map.put("msg","密码错误");
                map.put("username","密码错误");
                map.put("nickname","密码错误");
                map.put("Id",0);
            }
        }else {
            // 用户不存在
            map.put("code",0);
            map.put("msg","用户不存在");
            map.put("username","用户不存在");
            map.put("nickname","用户不存在");
            map.put("Id",0);
        }
        return map;
    }


}
